package com.libre;

import com.libre.constant.SysConstant;
import com.libre.view.HomeView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 * */
@SpringBootApplication
public class LibrePrintApplication extends AbstractJavaFxApplicationSupport {

    private static Label infoLb;

    @Override
    public void start(Stage stage) {
        // 主题
        // Application.setUserAgentStylesheet(SysConstant.THEME_STYLE);
        // Application.setUserAgentStylesheet(STYLESHEET_CASPIAN);

        stage.getIcons().add(new Image(SysConstant.LOGO_IMAGE));//任务栏图标

        Image image = new Image(SysConstant.BG_IMAGE);
        ImageView view = new ImageView(image);

        infoLb = new Label();
        infoLb.setTextFill(Color.WHITE);
        AnchorPane.setRightAnchor(infoLb, 10.0);
        AnchorPane.setBottomAnchor(infoLb, 10.0);
        AnchorPane pane = new AnchorPane();

        pane.getChildren().addAll(view, infoLb);
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.setWidth(image.getWidth());
        stage.setHeight(image.getHeight());
        stage.initStyle(StageStyle.UNDECORATED);
        stage.show();

        // 核心代码
        new Thread(() -> {
            initSystem();// 1

            Platform.runLater(() -> {// 2
                try {
                    HomeView home = new HomeView();
                    home.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> {
                        stage.hide();
                    });
                    home.show();
                } catch (Throwable e) {
                    e.printStackTrace();// 3
                }
            });
        }).start();
    }

    // 初始化系统
    private void initSystem() {
        try {
            showInfo("初始化目录...");
            Thread.sleep(500);
            showInfo("初始化系统配置...");
            Thread.sleep(500);
            showInfo("版本检测...");
            Thread.sleep(500);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    // 显示信息
    public static void showInfo(String info) {
        Platform.runLater(() -> {
                    infoLb.setTextFill(Color.valueOf("#9933FF"));
                    infoLb.setText(info);
                }
        );
    }

    public static void main(String[] args) {
        launch(args);
    }

}
